# -*- coding:utf-8 -*-

import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import timedelta
import datetime
import requests
from common.my_time import MyTime
from common.attr_v3 import AttrV3
from database.db_handler import MysqlHander
import json
import os
from selenium import webdriver

from webdriver_base import WebdriverBase
from common.my_file import MyFile

'''
北交所监管数据
'''

'''
chromedriver = "D:\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(executable_path="D:\\chromedriver.exe")

file = open('./out/shenjiaosuo_jianguan_data.txt', 'w', encoding = 'utf-8')
'''

class ShenjiaosuoJianguan(WebdriverBase):
    def __init__(self):
        self.v3 = AttrV3()
        self.file = open('./txt/' + os.path.split(__file__)[-1].split(".")[0] + ".txt", 'w', encoding = 'utf-8')
        WebdriverBase.__init__(self, './log/' + os.path.split(__file__)[-1].split(".")[0] + ".log")

    def __del__(self):
        self.file.close()
        pass

    def query_page_data(self, page):
        trs = self.driver.find_element_by_class_name("reporttboverfow-in").find_element_by_tag_name("tbody").find_elements_by_tag_name("tr")
        for i in range (0, len(trs)):
            #[stale element reference: element is not attached to the page document]需要重新定位以下元素
            tds = self.driver.find_element_by_class_name("reporttboverfow-in").find_element_by_tag_name("tbody").find_elements_by_tag_name("tr")[i].find_elements_by_tag_name("td")
            data_code = self.driver.find_element_by_class_name("reporttboverfow-in").find_element_by_tag_name("tbody").find_elements_by_tag_name("tr")[i].find_elements_by_tag_name("td")[0].text
            data_title = (self.driver.find_element_by_class_name("reporttboverfow-in").find_element_by_tag_name("tbody").find_elements_by_tag_name("tr")[i].find_elements_by_tag_name("td")[1].text + 
                         self.driver.find_element_by_class_name("reporttboverfow-in").find_element_by_tag_name("tbody").find_elements_by_tag_name("tr")[i].find_elements_by_tag_name("td")[3].text)
            data_date = self.driver.find_element_by_class_name("reporttboverfow-in").find_element_by_tag_name("tbody").find_elements_by_tag_name("tr")[i].find_elements_by_tag_name("td")[2].text
            data_url = "http://reportdocs.static.szse.cn" + self.driver.find_element_by_class_name("reporttboverfow-in").find_element_by_tag_name("tbody").find_elements_by_tag_name("tr")[i].find_elements_by_tag_name("td")[4].find_element_by_tag_name("a").get_attribute("encode-open")
            print(data_url)
            table = self.v3.get_table_name(3002)
            try:
                self.v3.add_attr_data(table, data_code, data_title, data_date, data_url, "深交所")
            except Exception as e:
                self.write_log(str(e))
                pass
            self.file.write(data_code + "|0|3002|" + data_title + "|" + data_date + "|" + data_url + "|深交所|\n")
        next_page = self.driver.find_element_by_class_name("next")
        if next_page:
            next_page.click()
            return True
        else:
            return False
 
    def query_data(self):
        self.driver.get("http://www.szse.cn/disclosure/supervision/measure/measure/index.html")
        time.sleep(10)
        page = 0
        while(self.query_page_data(page)):
            page = page + 1
            if page > 5:
                break
            print(page)
            time.sleep(3)
        self.driver.quit()
    
    def get_data(self):
        self.query_data()

def shenjiaosuo_jianguan_api():
    d = ShenjiaosuoJianguan()
    d.get_data()


if __name__ == '__main__':
    d = ShenjiaosuoJianguan()
    d.get_data()

